
$(function() {

	$("input[id^='code_s']:visible").autocomplete(Test.customer_code);

	$("select[id^='continent_s']:visible").selectChain({
		target: $("select[id^='country_s']:visible"),
		key: "code",
		value: "name",
		url: 'info.php?do=Country.ajax_continent',
		data: { ajax: true }
	}).trigger('change');

	Tip.emp('业务员');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("业务员：");
		} else if (a == 'oper') {
			label.html("录入员：");
		} else if (a == 'bj') {
			label.html("报价员：");
		}
	});
});


var Customer = {
	
	dialog_add_2: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				if (!Contact.validate_add()) {
					return false;
				}
								
				var linkman = $("input[name='linkman']");
				var job = $("input[name='job']");
				var tel = $("input[name='tel']");
				var fax = $("input[name='fax']");
				
				var mobile = $("input[name='mobile']");
				var email = $("input[name='email']");
				var address = $("input[name='address']");
				
				var qq = $("input[name='qq']");
				var skype = $("input[name='skype']");
				var content = $("input[name='content']");

				var aa = new Array();
				for (var i = 0; i < linkman.length; i++) {
					var contact = {};
					contact.linkman = $(linkman[i]).val();
					contact.job = $(job[i]).val();
					contact.tel = $(tel[i]).val();
					contact.fax = $(fax[i]).val();
					
					contact.mobile = $(mobile[i]).val();
					contact.email = $(email[i]).val();
					contact.address = $(address[i]).val();
					
					contact.qq = $(qq[i]).val();
					contact.skype = $(skype[i]).val();
					contact.content = $(content[i]).val();

					aa.push(contact);
				}

				var form = $("#form_add")[0];

				var body = { };
				body.code = form.code.value;
				body.code_t = form.code_t.value;
				body.name = form.name.value;
				body.fullname = form.fullname.value;
				body.continent = form.continent.value;
				body.country = form.country.value;

				body.address = form.address.value;
				body.url = form.url.value;
				body.jj = form.jj.value;

				body.contact = $.toJSON(aa);
				
				$(this).dialog("close");

				$.post('contact.php?do=Customer.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.code = form.code.value;
				body.code_t = form.code_t.value;
				body.name = form.name.value;
				body.fullname = form.fullname.value;
				body.cert_type = form.cert_type.value;
				body.cert_no = form.cert_no.value;
				
				body.continent = form.continent.value;
				body.country = form.country.value;

				body.address = form.address.value;
				body.url = form.url.value;
				body.jj = form.jj.value;
				
				$(this).dialog("close");

				$.post('contact.php?do=Customer.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	popup: function (e, p2, id, status) {
		$.cookie('id_', id);
		
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'contact.php?do=Customer.edit&id=' + id;	
		var c2 = 'contact.php?do=Customer.view&id=' + id;
		var c3 = 'contact.php?do=Customer.bj&id=' + id + p2;
		
		var c4 = 'contact.php?do=Customer.bank&id=' + id;	
		
		var c9 = 'contact.php?do=Customer.connect&id=' + id;

		var a1 = ['客户编辑', 'url', 'Customer.dialog_edit_', '650x510'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];
		var a3 = ['指定报价', 'url', 'Customer.dialog_bj', '600x360'];	
		
		var a4 = ['银行帐号', 'url', 'Customer.dialog_bank', '650x450'];
		
		var a9 = ['转移客户', 'url', 'Customer.dialog_connect', '650x400'];
		
		var s = c1 + ", " + c2 + ", " + c3 + ", " + c9;

		return this.popup_(e, [a1, a2, a3, a9], s);
	},
	
	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var c8 = 'contact.php?do=Customer.upload&id=' + id;
		var a8 = ['上传附件', 'url', 'Common.dialog_upload', '600x420'];
		
		var ss = 'contact.php?do=Customer.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Customer.dialog_ss', '400x200'];
		
		s += ", " + c8 + ", " + ss;
		
		a.push(a8);
		a.push(bb);
		
		return popup(e, a, s);
	},
	
	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('contact.php?do=Customer.audit&item=88', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},
	
					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
	
						var form = $("#form_edit")[0];
	
						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;
	
						$(this).dialog("close");
	
						var url = 'contact.php?do=Customer.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
	
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
	
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"修改": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				if (!ContactEdit.validate_edit()) {
					return false;
				}

				var id = $("input[name='id']");
				var linkman = $("input[name='linkman']");
				var job = $("input[name='job']");
				var dept = $("input[name='dept']");
				
				var tel = $("input[name='tel']");
				var fax = $("input[name='fax']");
				
				var mobile = $("input[name='mobile']");
				var email = $("input[name='email']");
				var address = $("input[name='address']");
				var geo = $("input[name='geo']");
				
				var qq = $("input[name='qq']");
				var skype = $("input[name='skype']");
				var content = $("input[name='content']");

				var aa = new Array();
				for (var i = 0; i < linkman.length; i++) {
					var contact = {};
					var b = $("#form" + z[i] + " :input[name='id']");
					if (b != null) {
						contact.id = $("#form" + z[i] + " :input[name='id']").val();
					}

					contact.linkman = $(linkman[i]).val();	
					contact.job = $(job[i]).val();
					contact.dept = $(dept[i]).val();
					
					contact.tel = $(tel[i]).val();
					contact.fax = $(fax[i]).val();
					
					contact.mobile = $(mobile[i]).val();
					contact.email = $(email[i]).val();
					contact.address = $(address[i]).val();
					contact.geo = $(geo[i]).val();
					
					contact.qq = $(qq[i]).val();
					contact.skype = $(skype[i]).val();
					contact.content = $(content[i]).val();
					
					aa.push(contact);
				}

				var form = $("#form_edit")[0];
				
				var body = { };		
				
				body.id = form.info_id.value;
				body.code = form.code.value;
				body.name = form.name.value;
				body.fullname = form.fullname.value;
				body.cert_type = form.cert_type.value;
				body.cert_no = form.cert_no.value;

				body.continent = form.continent.value;
				body.country = form.country.value;

				body.address = form.address.value;
				body.geo = form.main_geo.value;
				body.url = form.url.value;
				
				body.jj = form.jj.value;
				body.status = form.status.value;

				body.contact = $.toJSON(aa);
				
				body.z = $.cookie('contact');				
				$.cookie('contact', null);
				
				$(this).dialog("close");

				var url = 'contact.php?do=Customer.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	dialog_bj: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"指定": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				body.id = form.id.value;
				body.bj = form.bj.value;

				$(this).dialog("close");

				var url = 'contact.php?do=Customer.bjPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	dialog_bank: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {			
			"关闭": function() {
				$(this).dialog("close");
			},
	
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
							
				var form = $("#form_add")[0];
	
				var body = { };
				body.id = form.id.value;
				
				body.category = form.category.value;
				body.bank = form.bank.value;
				body.branch = form.branch.value;
	
				body.province = form.province.value;
				body.city = form.city.value;
				
				body.zone = form.zone.value;
				body.type = form.type.value;
				
				body.swift = form.swift.value;
				body.name = form.name.value;
				body.code = form.code.value;
				
				body.content = form.content.value;
				
				var url = 'contact.php?do=Customer.ajax_bank';
				$.getJSON(url, body, function(json) {
					if (json.status == '1') {
						$.post('contact.php?do=Customer.bankPost', body, function(data) {
							$('#dialog_add').html(data);
						});
					} else {
						// alert($.toJSON(json));
						alert("帐号信息已存在，请联系管理员！");
					}					
				});
	
				
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	dialog_connect: function (url, title, xy) {
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"转移": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				body.id = form.id.value;
				body.to_emp = form.to_emp.value;

				$(this).dialog("close");
				
				var url = 'contact.php?do=Customer.connectPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	download: function (key) {
		if (!confirm("确认是否下载？若下载，建议先另存，再打开。")) {
			return false;
		}
		
		var url = 'car.php?do=Car.download&key=' + key;
		jswin(url, 'save', '800x600');
		
		return true;
	},
	
	file: function (id, key) {
		if (!confirm("确认是否删除？")) {
			return false;
		}
		
		var c6 = 'contact.php?do=Customer.upload&id=' + id;
		
		var url = 'contact.php?do=Customer.file';
		var body = { };
		body.key = key;
		
		$.post(url, body, function(data) {
			// alert(data);
			$.get(c6, function(data) {
				$('#dialog_edit').html(data);
			});
		});
		
		return true;
	},
	
	customer_x: function(form) {		
		var customer_x = {};				
		customer_x.name = form.a_name.value;
		customer_x.fullname = form.a_fullname.value;
		customer_x.address = form.a_address.value;				
		customer_x.web = form.a_web.value;
		
		customer_x.contact = form.a_contact.value;
		customer_x.mobile = form.a_mobile.value;
		customer_x.tel = form.a_tel.value;
		customer_x.fax = form.a_fax.value;
		customer_x.email = form.a_email.value;
		customer_x.content = form.a_content.value;		
		
		return $.toJSON(customer_x);
	},
	
	linkman: function (a) {		
		$select = $("#a_linkman");		
		$select.empty();
		count = 0;
		$.each(a, function(k, v) {
	    	var item = new Option(v.name, v.id);
	    	$select[0].options.add(item);
	    	
	    	if (count == 1) {
	    		Customer._link_man(v.id, false);
	    	}
	    	
	    	count++;
	    });
		
		$select.change(function() {
			var id = $(this).val();
			Customer._link_man(id, true);
		}); 
	},

	_link_man: function (id, select) {
		var url = 'info.php?do=Contact.ajax_id&q=' + id;
		$.getJSON(url, function(json) {
			var aaa = Customer.aaa();
			if (select || aaa) {
				$("#a_contact").val(json.name);
				$("#a_mobile").val(json.mobile);
				$("#a_tel").val(json.tel);
				$("#a_fax").val(json.fax);
				$("#a_email").val(json.email);
			}
		});
	},
	
	aaa: function() {
		if ($("#a_contact").val().trim() != "") {
			return false;
		}
		 
		if ($("#a_mobile").val().trim() != "") {
			return false;
		} 
		
		if ($("#a_tel").val().trim() != "") {
			return false;
		}
		
		if ($("#a_fax").val().trim() != "") {
			return false;
		}
		
		if ($("#a_email").val().trim() != "") {
			return false;
		}
		
		return true;
	}
};
